<?php
/**
 * Created by PhpStorm.
 * User: 68008000
 * Date: 2017/11/16
 * Time: 11:06
 */

namespace app\index\controller;


use app\common\model\Category;
use think\Controller;

/**
 * Class Lists
 * @package app\index\controller
 * 列表控制器
 */
class Lists extends Common
{
    //列表首页
    public function index(){
        $cid = input('param.cid');
        $tid = input('param.tid');
        //分类进行处理的数据
        if($cid){
            //1.处理标签页title
            $conf = ['title'=>'博客-分类页'];

            //3.获取所有的子集
            $cids = (new Category())->getSon(db('category')->select(),$cid);
            $cids[] = $cid;

            //2.处理headData数据
            $headData = [
                'title'=>'分类',
                'name'=>db('category')->where('cid',$cid)->value('cname'),
                'total'=>db('article')->whereIn('category_cid',$cids)->count()
            ];

            //4.获取文章数据
            $articleData = db('article')
                ->alias('a')
                ->join('__CATEGORY__ c','a.category_cid = c.cid')
                ->whereIn('a.category_cid',$cids)
                ->where('is_recycle',1)
                ->order('sendtime desc')
                ->select();
        }

        //标签进来处理的数据
        if($tid){
            $conf = ['title'=>'博客-标签页'];

            //2.处理headData数据
            $headData = [
                'title'=>'标签',
                'name'=>db('tag')->where('tid',$tid)->value('tname'),
                'total'=>db('article_tag')->where('tag_tid',$tid)->count()
            ];

            //3.获取文章数据
            $articleData = db('article')
                ->alias('a')
                ->join('__ARTICLE_TAG__ at','a.aid =at.article_aid')
                ->join('__CATEGORY__ c','a.category_cid = c.cid')
                ->where('at.tag_tid',$tid)
                ->where('is_recycle',1)
                ->order('sendtime desc')
                ->select();
        }

        //给文章数据统一加上标签
        foreach ($articleData as $k=>$v){
            $articleData[$k]['tags'] = db('article_tag')
                ->alias('at')
                ->join('__TAG__ t','at.tag_tid = t.tid')
                ->where('at.article_aid',$v['aid'])
                ->field(['t.tid,t.tname'])
                ->select();
        }

        $this->assign('conf',$conf);
        $this->assign('headData',$headData);
        $this->assign('articleData',$articleData);
        //视图
        return view();
    }
}